package com.three.crm.page.goods;



import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.three.crm.R;
import com.three.crm.adapter.MyFragmentStateAdapter;
import com.three.crm.base.BaseActivity;
import com.three.crm.fragment.ArrivalFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 到货信息
 * @created by lqd on 2024-03-02.
 */
public class ArrivalListActivity extends BaseActivity {

    private TextView tvTitle;
    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private List<Fragment> fragmentList;
    private String[] tabString = new String[]{"未到货","已到货"};
    private int currentIndex = 0;

    @Override
    protected void initParms(Bundle bundle) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_arrival_list;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText("到货信息");
        viewPager = $(R.id.viewpager);
        tabLayout = $(R.id.tablayout);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentIndex = position;
                viewPager.setCurrentItem(position,true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
        }
    }

    @Override
    protected void initData(Context mContext) {
        fragmentList = new ArrayList<>();
        for (int i = 0; i < tabString.length; i++) {
            fragmentList.add(ArrivalFragment.newInstance(i + 1,tabString[i]));
        }
        viewPager.setAdapter(new MyFragmentStateAdapter(getSupportFragmentManager(),getLifecycle(),fragmentList));
        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabString[position]);
            }
        }).attach();
        viewPager.setCurrentItem(currentIndex,true);
    }
}